﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of PinButton class
	/// A button that represents a pin, used to pin menus and panels
	/// </summary>
	public partial class PinButton : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Delegates and events

		public event EventHandler PinnedStateChange;

		/////////////////////////////////////////////////////////////
		// Dependency properties

		public static readonly DependencyProperty PinnedStateProperty;

		/////////////////////////////////////////////////////////////
		// Attributes

		public bool PinnedState
		{
			get
			{
				return (bool)GetValue(PinnedStateProperty);
			}
			set
			{
				SetValue(PinnedStateProperty, value);
			}
		}

		/////////////////////////////////////////////////////////////
		// Construction

		static PinButton()
		{

			// Register dependency properties
			PinnedStateProperty = DependencyProperty.Register(
				"PinnedState",
				typeof(bool),
				typeof(PinButton),
				new PropertyMetadata(true, PinnedStateChanged)
			);

		}

		public PinButton()
		{

			// Load xaml resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Dependency property - Event handlers

		private static void PinnedStateChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
		{

			// Set inner alignment
			((PinButton)element)._setPinnedState((bool)e.NewValue);

		}

		/////////////////////////////////////////////////////////////
		// Events

		private void MainButton_Click(object sender, RoutedEventArgs e)
		{

			// Toggle the state
			_setPinnedState(!PinnedState);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _setPinnedState(bool bPinned)
		{

			// Show/hide pin
			InnerCircle.Visibility = bPinned ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;

			// Update state
			PinnedState = bPinned;

			// Fire event
			if (PinnedStateChange != null)
				PinnedStateChange(this, new EventArgs());

		}

	}

}
